package com.think.teaching.controller;

import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.Major;
import com.think.teaching.service.impl.MajorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 专业 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "专业类型管理接口")
public class MajorController {

	private final MajorServiceImpl majorService;

	public MajorController(MajorServiceImpl majorService) {
		this.majorService = majorService;
	}

	@ApiOperation("添加新的专业信息")
	@PostMapping("admin/major")
	public Result AddNewMajor(@RequestBody() Major data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Major major = majorService.AddNewMajor(data);
			if (major != null) {
				return Result.success(major);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("更新专业信息")
	@RequestMapping(value = "admin/major", method = RequestMethod.PUT)
	public Result UpdateMajor(@RequestBody() Major data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Major major = majorService.UpdateMajor(data);
			if (major != null) {
				return Result.success(major);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("删除专业信息")
	@RequestMapping(value = "admin/major/{id}", method = RequestMethod.DELETE)
	public Result DeleteMajor(@PathVariable("id") Integer id) {
		Major major = majorService.getById(id);
		if (major == null) {
			return Result.failure(ResultCode.MAJOR_NOT_EXIST);
		} else {
			if (majorService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("管理员获取专业信息详情")
	@RequestMapping(value = "admin/major/{id}", method = RequestMethod.GET)
	public Result GetMajor(@PathVariable("id") Integer id) {
		Major major = majorService.getById(id);
		if (major == null) {
			return Result.failure(ResultCode.MAJOR_NOT_EXIST);
		} else {

			return Result.success(major);
		}
	}

	@ApiOperation("管理员修改专业信息状态")
	@RequestMapping(value = "admin/major/state", method = RequestMethod.PUT)
	public Result UpdateMajorState(@RequestParam("id") Integer id, @RequestParam("state") Integer state) {
		Major major = majorService.getById(id);
		if (major == null) {
			return Result.failure(ResultCode.MAJOR_NOT_EXIST);
		} else {
			try {
				major = majorService.SetMajorState(id, state);
				return Result.success(major);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索专业信息")
	@RequestMapping(value = "user/major/search", method = RequestMethod.GET)
	public Result UserSearchMajor(@RequestParam("key") String key) {
		return Result.success(majorService.SearchMajorWithKey(key, true));
	}
	
	@ApiOperation("用户获取专业下的所有班级信息")
	@RequestMapping(value = "user/major/classes/{id}", method = RequestMethod.GET)
	public Result UserGetMajorClasses(@PathVariable("id") Integer id) {
		Major major = majorService.getById(id);
		if (major == null) {
			return Result.failure(ResultCode.MAJOR_NOT_EXIST);
		} else {
			try {
				return Result.success(majorService.GetMajorClasses(id));
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("用户获取专业下的所有班级信息")
	@RequestMapping(value = "admin/major/student/{id}", method = RequestMethod.GET)
	public Result UserGetMajorStudentss(@PathVariable("id") Integer id) {
		Major major = majorService.getById(id);
		if (major == null) {
			return Result.failure(ResultCode.MAJOR_NOT_EXIST);
		} else {
			try {
				return Result.success(majorService.GetMajorStudents(id));
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("管理员搜索专业信息")
	@RequestMapping(value = "admin/major/search", method = RequestMethod.GET)
	public Result AdminSearchMajor(@RequestParam("key") String key) {
		return Result.success(majorService.SearchMajorWithKey(key, true));
	}

	@ApiOperation("管理员搜索专业信息列表")
	@RequestMapping(value = "admin/majors/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListMajor(
			@PathVariable("page") Integer page, 
			@PathVariable("count") Integer count,
			@RequestParam(value = "college", defaultValue = "-1") Integer college,
			@RequestParam(value = "school", defaultValue = "-1") Integer school) {
		return Result.success(majorService.ListMajorWithCollege(count, page, college,school, false));
	}

	@ApiOperation("用户搜索专业信息列表")
	@RequestMapping(value = "user/majors/{page}/{count}", method = RequestMethod.GET)
	public Result UserListMajor(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(value = "college", defaultValue = "-1") Integer college,
			@RequestParam(value = "school", defaultValue = "-1") Integer school) {
		return Result.success(majorService.ListMajorWithCollege(count, page, college,school, true));
	}
}
